package com.zwy.space.controller.admin;


import com.zwy.space.domain.User;
import com.zwy.space.queryVo.UserVo;
import com.zwy.space.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService userService;


    @RequestMapping
    public String loginPage(){
        return "admin/login";
    }

    @RequestMapping("/logout")
    public String logOut(){
        return "redirect:/admin";
    }


    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes attributes
                        ){

        UserVo user=userService.checkUser(username,password);
        if(user!=null){
//            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        }else{
            attributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }
    }

}
